package com.tfj.mvp.tfj.home.news;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.hyphenate.tfj.R;
import com.jakewharton.rxbinding2.view.RxView;
import com.tfj.comm.bean.Constant;
import com.tfj.mvp.base.BaseActivity;
import com.tfj.mvp.tfj.home.news.bean.NewsBean;
import com.tfj.mvp.tfj.live.bean.TagBean;
import com.tfj.utils.SysUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Liang_Lu on 2017/12/21.
 *
 * @author HuangQiang
 * @github https://github.com/HuangQiang
 */

public class VNewsListActivity extends BaseActivity<PNewsListImpl> implements CNewsList.IVNewsList {


    @BindView(R.id.back_titlebar_iv)
    ImageView backTitlebarIv;
    @BindView(R.id.editText_search)
    EditText editTextSearch;
    @BindView(R.id.recyclew_tag)
    RecyclerView recyclewTag;
    @BindView(R.id.recyclew_content)
    RecyclerView recyclewContent;
    private int widthItem= 0 ;
    @Override
    protected void initView() {
        super.initView();
        widthItem = SysUtils.getScreenWidth(this)/2-SysUtils.dip2px(this,48)/2;
        //标签
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclewTag.setLayoutManager(linearLayoutManager);
        recyclewTag.setAdapter(quickAdapter_tag);
        List<TagBean> tagBeans = new ArrayList<>();
        tagBeans.add(new TagBean("全部"));
        tagBeans.add(new TagBean("新闻百科"));
        tagBeans.add(new TagBean("房产资讯"));
        tagBeans.add(new TagBean("精彩图说"));
        quickAdapter_tag.replaceData(tagBeans);
        //新闻
        LinearLayoutManager linearLayoutManager_news  =new LinearLayoutManager(this);
        recyclewContent.setLayoutManager(linearLayoutManager_news);
        recyclewContent.setAdapter(quickAdapter_news);
        List<NewsBean> newsBeans = new ArrayList<NewsBean>();
        newsBeans.add(new NewsBean());
        newsBeans.add(new NewsBean());
        newsBeans.add(new NewsBean());
        newsBeans.add(new NewsBean());
        newsBeans.add(new NewsBean());
        quickAdapter_news.replaceData(newsBeans);

    }

    @Override
    public int setContentViewId() {
        return R.layout.activity_newslist;
    }

    @Override
    public void createPresenter() {
        mPresenter = new PNewsListImpl(mContext, this);
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    private int checkPosition = 0;
    private QuickAdapter_Tag quickAdapter_tag = new QuickAdapter_Tag();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    public class QuickAdapter_Tag extends BaseQuickAdapter<TagBean, BaseViewHolder> {
        public QuickAdapter_Tag() {
            super(R.layout.item_tag);
        }

        @Override
        protected void convert(BaseViewHolder viewHolder, final TagBean item) {
            RelativeLayout rl_out = viewHolder.getView(R.id.rl_out);
            TextView textView_name = viewHolder.getView(R.id.textView_name);
            textView_name.setText(item.getName());
            textView_name.setTextColor(viewHolder.getPosition() == checkPosition ? Color.parseColor("#ffffff") : Color.parseColor("#ffbfc2cc"));
            rl_out.setBackgroundResource(viewHolder.getPosition() == checkPosition ? R.drawable.shape_background_main_2 : R.drawable.shape_background_gray_2);
            addDisposable(RxView.clicks(rl_out).throttleFirst(Constant.SECONDS_FORBID, TimeUnit.SECONDS)
                    .subscribe(o -> {
                        checkPosition = viewHolder.getPosition();
                        quickAdapter_tag.notifyDataSetChanged();
                    }));
        }
    }
    private QuickAdapter_News  quickAdapter_news = new QuickAdapter_News();

    public class QuickAdapter_News extends BaseQuickAdapter<NewsBean, BaseViewHolder> {
        public QuickAdapter_News() {
            super(R.layout.item_news);
        }

        @Override
        protected void convert(BaseViewHolder viewHolder, final NewsBean item) {
            RelativeLayout rl_out = viewHolder.getView(R.id.rl_out);
            rl_out.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(VNewDetailActivity.class);
                }
            });
            ImageView imageView_left = viewHolder.getView(R.id.iamgeView_left);
            ViewGroup.LayoutParams layoutParams_img = imageView_left.getLayoutParams();
            layoutParams_img.width = widthItem;
            imageView_left.setLayoutParams(layoutParams_img);
        }
    }

}
